/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sunsite.dao;

import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import sunsite.po.News;

/**
 *
 * @author mg
 */
public class NewsDaoImpl extends HibernateDaoSupport implements NewsDao {

    private int maxResult = 5;

    public List<News> getTop5News() throws DataAccessException {
        List<News> list = getHibernateTemplate().executeFind(new HibernateCallback() {

            public Object doInHibernate(Session arg0) throws HibernateException, SQLException {
                return arg0.createQuery("from News as n order by n.releasedate").setMaxResults(5).list();
            }
        });
        return list;
    }

    public List<News> getNews(final int pageNow, final int newsType) throws DataAccessException {
        List<News> list = getHibernateTemplate().executeFind(new HibernateCallback() {

            public Object doInHibernate(Session arg0) throws HibernateException, SQLException {
                Query q = null;
                if (newsType < 0) {
                    q = arg0.createQuery("from News as n order by n.releasedate desc");
                } else {
                    q = arg0.createQuery("from News as n where n.newsType.newsTypeid=? order by n.releasedate desc").setParameter(0, newsType);
                }
                q.setFirstResult((pageNow - 1) * maxResult);
                q.setMaxResults(maxResult);
                return q.list();
            }
        });
        if (list != null && list.size() > 0) {
            return list;
        } else {
            return null;
        }
    }

    public int getPageCount(final int newsType) throws DataAccessException {
        int pageCount = 0;
        int count = ((Long) (getHibernateTemplate().executeFind(new HibernateCallback() {

            public Object doInHibernate(Session arg0) throws HibernateException, SQLException {
                Query q = null;
                if (newsType < 0) {
                    q = arg0.createQuery("select count(n.newsId) from News as n");
                } else {
                    q = arg0.createQuery("select count(n.newsId) from News as n where n.newsType.newsTypeid=?").setParameter(0, newsType);
                }
                return q.list();
            }
        }).get(0))).intValue();
        if (count <= maxResult) {
            pageCount = 1;
            return pageCount;
        }
        if (count % maxResult > 0) {
            pageCount = count / maxResult + 1;
        } else {
            pageCount = count / maxResult;
        }
        return pageCount;
    }

    public News getNewsById(String id) throws DataAccessException {
        return (News)getHibernateTemplate().get(News.class, id);
    }
}
